import http.client, urllib.request, urllib.parse, urllib.error, base64
import json


class GetData:
    def __init__(self):
        self.university_name = ''
        self.offset = None
        self.Y = 0
        self.university_paper_data_json = {
            # And(composite(AA.AfN=='tsinghua university'),Y=2018)
            'expr': "And(composite(AA.AfN=='" + self.university_name + "'),Y=%d)" % self.Y,
            'entities': []
        }

    def get_data_from_api(self, university_name, offset, Y):
        self.university_name = university_name
        self.offset = offset
        self.Y = Y
        self.university_paper_data_json = self.university_paper_data_json
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': '3d4da2c07e63414eb9432f493955abe4',
        }
        params = urllib.parse.urlencode({
            # Request parameters
            'expr': "And(composite(AA.AfN=='" + self.university_name + "'),Y=%d)" % self.Y,
            'model': 'latest',
            'count': '500',
            'offset': self.offset,
            'orderby': 'Y',
            'attributes': 'Id,Ti,Y,CC,ECC,AA.AfId,AA.AfN,AA.AuId,AA.AuN,AA.S,F.FId,F.FN,FC.FId,FC.FN,FP.FId,FP.FN,PC,RId',
        })

        try:
            conn = http.client.HTTPSConnection('api.labs.cognitive.microsoft.com')
            conn.request("GET", "/academic/v1.0/evaluate?%s" % params, "", headers)
            response = conn.getresponse()
            # data = response.read()
            # print(data)
            # response = conn.getresponse()
            print(response.status)
            if response.status == 200:
                data_str = str(response.read(), encoding="utf-8")
                data_json = json.loads(data_str)
                data_list = data_json['entities']
                # 每篇论文对应得信息，格式为dict
                # for each_paper_info in data_list:
                #     print(each_paper_info)
                return data_list
            conn.close()
        except Exception as e:
            print("[Errno {0}] {1}".format(e.errno, e.strerror))

    # def get_data_from_api2(self, university_name, offset, Y):
    #     self.university_name = university_name
    #     self.offset = offset
    #     self.Y = Y
    #     self.university_paper_data_json = self.university_paper_data_json
    #     headers = {
    #         # Request headers
    #         'Ocp-Apim-Subscription-Key': '3d4da2c07e63414eb9432f493955abe4',
    #     }
    #
    #     params = urllib.parse.urlencode({
    #         # Request parameters
    #         # 'expr': "And(composite(AA.AfN=='" + self.university_name + "'),Y=%d)" % self.Y,
    #         'model': 'latest',
    #         'count': '100',
    #         'offset': self.offset,
    #         'orderby': 'Y',
    #         'attributes': 'Ti,Y,CC,ECC,AA.AuN,AA.AuId,AA.AfN,AA.AfId,AA.S',
    #     })
    #
    #     try:
    #         conn = http.client.HTTPSConnection('api.labs.cognitive.microsoft.com')
    #         # conn.request("GET", "/academic/v1.0/evaluate?%s" % params, "", headers)
    #         expr = "And(composite(AA.AfN=='" + self.university_name + "'),Y=%d)" % self.Y
    #         conn.request("GET", "/academic/v1.0/evaluate?expr=%s" % expr + "&%s" % params, "", headers)
    #         response = conn.getresponse()
    #         print(response.status)
    #         if response.status == 200:
    #             data_str = str(response.read(), encoding="utf-8")
    #             data_json = json.loads(data_str)
    #             data_list = data_json['entities']
    #             # 每篇论文对应得信息，格式为dict
    #             for each_paper_info in data_list:
    #                 print(each_paper_info)
    #             return data_list
    #         conn.close()
    #     except Exception as e:
    #         print("[Errno {0}] {1}".format(e.errno, e.strerror))
